package com.mayikt.decorate.ext;

import com.mayikt.decorate.AbstractDecorate;
import com.mayikt.decorate.ComponentCache;
import com.mayikt.decorate.impl.JvmComponentCache;
import com.mayikt.entity.UserEntity;
import com.mayikt.utils.JvmMapCacheUtils;
import com.mayikt.utils.RedisUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName RedisDecorate
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
@Component
public class RedisDecorate extends JvmComponentCache implements AbstractDecorate {
    @Autowired
    private RedisUtils redisUtils;
//
//    @Autowired
//    private JvmComponentCache jvmComponentCache;

    @Override
    public <T> T getCacheEntity(String key, Class<T> t, ProceedingJoinPoint joinPoint) {
        // 查询我们的二级缓存
        // 先查询二级缓存
        T redisUser = redisUtils.getEntity(key, t);
        if (redisUser != null) {
            return (T) redisUser;
        }
        // 如果一级缓存存在数据
        T jvmUser = super.getCacheEntity(key, t, joinPoint);
        if (jvmUser == null) {
            return null;
        }
        // 将该缓存数据放入到二级缓存中
        redisUtils.putEntity(key, jvmUser);
        return (T) jvmUser;
    }
    /// 6 7
}
